﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class Pause : View
    {

        private string header, mainMenuString, quitGameString, resumeString;
        private SpriteFont headerFont, textFont;
        private Vector2 resumeGamePos, quitGamePos, headerPos, mainMenuPos;
        private Vector2 resumeOrigin, quitGameOrigin, headerOrigin, mainMenuOrigin;
        private int selectedEntry = 1;
        private int minEntry = 1;
        private int maxEntry = 3;

        public Pause()
            : base()
        {
            header = "Pause Menu";
            headerPos = new Vector2(Game.WindowWidth / 2, 35);

            resumeString = "Resume Game";
            resumeGamePos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 150);
            mainMenuString = "Main Menu";
            mainMenuPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 125);
            quitGameString = "Quit";
            quitGamePos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 50);
        }

        public override void LoadContent(ContentManager content)
        {
            headerFont = content.Load<SpriteFont>("Text");
            textFont = content.Load<SpriteFont>("Text");

            headerOrigin = headerFont.MeasureString(header) / 2;
            resumeOrigin = textFont.MeasureString(resumeString) / 2;
            mainMenuOrigin = textFont.MeasureString(mainMenuString) / 2;
            quitGameOrigin = textFont.MeasureString(quitGameString) / 2;
        }

        public override void Update(GameTime gameTime)
        {
            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.MenuDown)
            {
                if (selectedEntry + 1 > maxEntry)
                    selectedEntry = minEntry;
                else
                    selectedEntry++;
            }
            else if (Controls.MenuUp)
            {
                if (selectedEntry - 1 < minEntry)
                    selectedEntry = maxEntry;
                else
                    selectedEntry--;
            }

            if (Controls.Enter)
            {
                switch (selectedEntry)
                {
                    case 1:
                        ViewManager.State = GameState.InGame;
                        break;
                    case 2:
                        ViewManager.State = GameState.Menu;
                        selectedEntry = 1;
                        break;
                    case 3:
                        Game.GetInstance().Exit();
                        break;
                    default:
                        throw new ArgumentException("Invalid menu entry: " + selectedEntry);
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(headerFont, header, headerPos, Color.White, 0, headerOrigin, 1.0f, SpriteEffects.None, 0.5f);

            if (selectedEntry != 1)
                spriteBatch.DrawString(textFont, resumeString, resumeGamePos, Color.White, 0, resumeOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 2)
                spriteBatch.DrawString(textFont, mainMenuString, mainMenuPos, Color.White, 0, mainMenuOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 3)
                spriteBatch.DrawString(textFont, quitGameString, quitGamePos, Color.White, 0, quitGameOrigin, 1.0f, SpriteEffects.None, 0.5f);

            switch (selectedEntry)
            {
                case 1:
                    spriteBatch.DrawString(textFont, resumeString, resumeGamePos, Color.Red, 0, resumeOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 2:
                    spriteBatch.DrawString(textFont, mainMenuString, mainMenuPos, Color.Red, 0, mainMenuOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 3:
                    spriteBatch.DrawString(textFont, quitGameString, quitGamePos, Color.Red, 0, quitGameOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                default:
                    throw new ArgumentException("Invalid menu entry: " + selectedEntry);
            }
        }
    }
}
